<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns='http://www.w3.org/1999/xhtml' xml:lang='en' lang='en'>
	<head>
		<title>Install Traccia</title>
		
		<link rel='stylesheet' type='text/css' href='../inc/style.css' />
		<script type='text/javascript' src='../inc/jquery.js'></script>
		
		<script type='text/javascript'>

            function doError(element, flag)
            {
                // find the TR of the element
                while(element.tagName.toString() !== 'TR')
                {
                    element = element.parentNode;
                }
                
                if(flag)
                {
                    $(element).find('input').addClass('e_border');
                    $(element).find('th').addClass('e_text');
                }
                else
                {
                    $(element).find('input').removeClass('e_border');
                    $(element).find('th').removeClass('e_text');
                }
            }

			function installTraccia() 
			{	
				var form = document.forms['install'];				
				var fields = ['db_host', 'db_user', 'db_name', 'db_pref', 'admin_user', 'admin_pass', 'admin_conf'];
				var error = false;
				
				for(var i = 0; i < fields.length; i++)
				{
				    var field = form[fields[i]];
				    doError(field, 0);
				    if(field.value == '')
				    {
				        error = true;
				        doError(field, 1);
				    }
				}
				
				if(form.admin_pass.value !== form.admin_conf.value)
				{
				    error = true;
				    doError(form.admin_pass, 1);
				    doError(form.admin_conf, 1);
				}
				
				if(!error)
				{
				    $('a').css('visibility', 'hidden');
				    $.ajax(
				    {
				        url : 'install.php',
				        type : 'post',
				        cache : false,
				        data:
				        {
				            db_host : form.db_host.value,
				            db_user : form.db_user.value,
				            db_pass : form.db_pass.value,
				            db_name : form.db_name.value,
				            db_pref : form.db_pref.value,
				            admin_user : form.admin_user.value,
				            admin_pass : form.admin_pass.value
				        },
				        dataType : 'html',
				        success : function(data)
				        {
				            alert(data);
				            if(data.indexOf('Control Panel') > -1)
				            {
				                window.location.href = '../cp';
				            }
				            else
			                {
			                    $('a').css('visibility', 'visible');
			                }
				        }
				    });
				}
				
                /*
				// database
				if(Validate.empty(form.db_host)) return;
				if(Validate.empty(form.db_user)) return;
				if(Validate.empty(form.db_pass)) return;
				if(Validate.empty(form.db_name)) return;
				if(Validate.empty(form.db_pref)) return;

				// administrator
				if(Validate.empty(form.admin_user)) return;
				if(Validate.empty(form.admin_pass)) return;
				if(Validate.empty(form.admin_conf)) return;

				// must match
				if(form.admin_pass.value !== form.admin_conf.value) Validate.alert(form.admin_conf, 'Passwords do not match');

				$$('a')[0].setStyle('visibility', 'hidden');

				// send the ajax
				new Ajax({
					url:'install.php',
					method:'post',
					complete:function(data) {
						alert(data);
						$$('a')[0].setStyle('visibility', 'visible');
						if(data.indexOf('Control Panel') > -1)
							window.location.href = '../cp';
					}
				}).send('db_host=' + form.db_host.value +
						'&db_user=' + form.db_user.value +
						'&db_pass=' + form.db_pass.value +
						'&db_name=' + form.db_name.value +
						'&db_pref=' + form.db_pref.value +
						'&admin_user=' + form.admin_user.value +
						'&admin_pass=' + form.admin_pass.value +
						'&trace_path=' + form.trace_path.value);
			    */
			}

		</script>
	</head>
	<body class='center'>

		<div id='wrapper'>
			<div id='container'>
				<h2>Install <span>Traccia</span></h2>
				<p>Welcome to the one and only page for installing Traccia.</p>
				<p>This will create a file called <strong>config.php</strong> in the <strong><?php echo basename(dirname(dirname(__FILE__))); ?></strong> directory.</p>
				<p><small>Please follow the steps and set the fields accordingly.</small></p>

				<form name='install' onsubmit='return false'>

					<dl>
						<dt>Database</dt>
						<dd>
							<table>
								<tr>
									<th>
										Host
									</th>
									<td>
										<input type='text' name='db_host' value='localhost' />
									</td>
								</tr>
								<tr>
									<th>
										Username
									</th>
									<td>
										<input type='text' name='db_user' value='root' />
									</td>
								</tr>
								<tr>
									<th>
										Password
									</th>
									<td>
										<input type='password' name='db_pass' />
									</td>
								</tr>
								<tr>
									<th>
										Database Name
									</th>
									<td>
										<input type='text' name='db_name' value='traccia' />
									</td>
								</tr>
								<tr>
									<th>
										Table Prefix
									</th>
									<td>
										<input type='text' name='db_pref' value='traccia_' />
									</td>
								</tr>
							</table>
						</dd>
					</dl>

					<dl>
						<dt>Administrator</dt>
						<dd>
							<table>
								<tr>
									<th>
										Username
									</th>
									<td>
										<input type='text' name='admin_user' value='admin' />
									</td>
								</tr>
								<tr>
									<th>
										Password
									</th>
									<td>
										<input type='password' name='admin_pass' />
									</td>
								</tr>
								<tr>
									<th>
										Confirm
									</th>
									<td>
										<input type='password' name='admin_conf' />
									</td>
								</tr>
							</table>
						</dd>
					</dl>

					<a href='#' onclick='installTraccia();' class='button'>Install Traccia</a>

				</form>

			</div>
		</div>

	</body>
</html>